<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\model;

use think\Model;

/**
 * 标签和视频关联模型
 * @package app\user_center\model
 */
class VideoPlayLabelRelation extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_video_play_label_relation';

    /**
     * 获取视频标签
     * @param int $play_id 视频id
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function getListByPlayid($play_id){
        $map = [];
        $map['play_id'] = $play_id;
        $list = $this ->withJoin('getVideoPlayClassLabel', 'LEFT') -> where($map) -> select();
        if($list){
            return array_out(200, 'success', $list);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 添加数据
     * @param play_id 	int 视频id
     * @param label_id 	int 标签id
     * @author shiqiang
     * @return array_out(code, message, (int)id)
     */
    public function addData($play_id, $label_id){
        $this -> play_id          = $play_id;
        $this -> label_id         = $label_id;
        $res = $this -> isUpdate(false) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 添加数据
     * @param array $list 	标签
     * @author shiqiang <976162041@qq.com>
     * @return bool
     */
    public function addDataList($list){
        $res = $this -> isUpdate(false) -> saveAll($list);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 删除数据
     * @param int $id
     * @author shiqiang <976162041@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
	
    /**
     * 视频信息表 关联载入（一对一）
     */
    public function getVideoPlay(){
        return $this -> hasOne('app\video\model\VideoPlay','play_id', 'play_id');
    }

    /**
     * 视频标签 关联载入（一对多）
     */
    public function getVideoPlayClassLabel(){
        return $this -> hasOne('app\video\model\VideoPlayClassLabel','id', 'label_id');
    }
}
